<?php
namespace app\admin\controller;
//导入Controller
use think\Controller;
//导入Db
use think\Db;
class Cates extends Allow
{
    //分割列表
    public function getcates(){
        $cate=Db::query("select *,concat(path,',',id) as paths from cates order by paths");
        // var_dump($cate);exit;
        //遍历
        foreach($cate as $key=>$value){
            //获取path
            $path=$value['path'];
            //转换为数组
            $arr=explode(",",$path);
            //获取逗号个数
            $len=count($arr)-1;
            //添加分割符
            $cate[$key]['name']=str_repeat("--|",$len).$value['name'];
        }
        return $cate;
    }
	//列表
    public function getindex()
    {
        //调用分割函数
        $cate=$this->getcates();
        //查询列表数据
        // $cate=Db::query("select *,concat(path,',',id) as paths from cates order by paths");
       //加载分类列表
       return $this->fetch("Cates/index",['cate'=>$cate]);
    }
    //添加
    public function getadd(){
        //查询列表数据
        // $cate=Db::table('cates')->select();
        $cate=$this->getcates();
        
    	//加载添加模板
    	return $this->fetch("Cates/add",['cate'=>$cate]);
    }

    //执行添加
    public function postinsert(){
       //创建请求对象
        $request=request();
        // echo "<pre>";
        // var_dump($request->param());exit;
        $data=$request->only(['name','pid']);
        //获取pid
        $pid=$request->param("pid");
        //添加顶级分类
        if($pid==0){
            // echo "<pre>";
            // var_dump($data);
            //拼接path
            $data['path']="0";
        }else{
            //添加子类信息
            // echo "<pre>";
            // var_dump($data);
            //获取父类信息
            $info=Db::table("cates")->where("id","{$pid}")->find();
            //拼接path
            $data['path']=$info['path'].",".$info['id'];

        }
        //判断类名是否为空
        if(!empty($data['name'])){
            //执行插入
            if(Db::table("cates")->insert($data)){
                $this->success("数据添加成功","/admincates/index");
            }else{
                $this->error("数据添加失败","/admincates/add");
            }
        }else{
            $this->error("类名不能为空","/admincates/add");
        }
    }
    //删除    
    public function getdelete(){
        //获取删除数据的id
        $request=request();
        $id=$request->param('id');
        //获取当前类别下的子类个数
        $count=Db::table('cates')->where('pid',"{$id}")->Count();
        // echo $count;
        if($count>0){
            $this->error("请先删除子类","/admincates/index");
        }
        //直接删除
        if(Db::table('cates')->where("id","{$id}")->delete()){
            $this->success("数据删除成功","/admincates/index");
        }else{
            $this->error("数据删除失败","/admincates/index");
        }
    }

    //获取需要修改的数据
    public function getedit(){
        $request=request();
        //获取需要修改数据的id
        $id=$request->param('id');
        //获取需要修改的数据
        $info=Db::table('cates')->where("id","{$id}")->find();
        //获取所有类别
        $data=Db::table("cates")->select();
       //调用修改页面
        return $this->fetch("Cates/edit",['info'=>$info,'data'=>$data]);
    }

    //执行修改
    public function postupdate(){
        $request=request();
        $id=$request->param('id');
        //获取修改数据
        $data=$request->only(['name']);
        //执行修改
        if(Db::table('cates')->where("id","{$id}")->update($data)){
            $this->success("修改成功","/admincates/index");
        }else{
            $this->error("修改失败","/admincates/edit");
        }
    }
 
}
